 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.Price;
 import com.oanda.blackberry.restapi.Quote;
 import com.oanda.blackberry.restapi.Rate;
 import com.oanda.blackberry.restapi.Symbol;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.Settings;
 import net.rim.device.api.system.Bitmap;
 import net.rim.device.api.ui.Font;
 import net.rim.device.api.ui.Graphics;
 import net.rim.device.api.ui.component.ListField;
 
 public class RatesListField
 {
   private Quote[] listData;
   private ListField listField;
   private ListCallBack listFieldCallBack = null;
   int[] quoteState;
   static final int QUOTE_NOCHANGE = 0;
   static final int QUOTE_GOINGUP = 1;
   static final int QUOTE_GOINGDOWN = 2;
 
   public RatesListField(Rate rates)
   {
     this.listFieldCallBack = new ListCallBack();
 
     this.listField = new ListField() {
       protected void drawFocus(Graphics graphics, boolean on) {
       }
 
       protected int moveFocus(int amount, int status, int time) {
         int ret = super.moveFocus(amount, status, time);
         invalidate();
         return ret;
       }
 
       protected void moveFocus(int x, int y, int status, int time) {
         int oldSelection = getSelectedIndex();
         super.moveFocus(x, y, status, time);
         int newSelection = getSelectedIndex();
 
         if (oldSelection != -1) {
           invalidate(oldSelection);
         }
 
         if (newSelection != -1) {
           invalidate(newSelection);
         } else {
           setSelectedIndex(oldSelection);
           invalidate(oldSelection);
         }
       }
     };
     this.listField.setEmptyString("No quotes!", 6);
     this.listData = rates.getQuotes();
     this.listField.setRowHeight(48);
 
     this.listField.setCallback(this.listFieldCallBack);
 
     int elementLength = rates.getQuotes().length;
     this.quoteState = new int[elementLength];
     for (int count = 0; count < elementLength; count++) {
       this.listField.insert(count);
       this.quoteState[count] = 0;
     }
   }
 
   public ListField getList()
   {
     return this.listField;
   }
 
   public Quote getQuoteSelected() {
     int index = this.listField.getSelectedIndex();
     if ((this.listData != null) && (index != -1) && (index < this.listData.length)) {
       Quote data = this.listData[index];
       return data;
     }
 
     return null;
   }
 
   public Quote[] getQuotes()
   {
     return this.listData;
   }
 
   public void setQuote(Quote quote) {
     for (int count = 0; count < this.listData.length; count++)
     {
       Quote quoteOld = this.listData[count];
 
       if (!quote.getSymbol().equals(quoteOld.getSymbol()))
         continue;
       this.listData[count] = quote;
 
       if (quote.getBid() > quoteOld.getBid())
         this.quoteState[count] = 1;
       else if (quote.getBid() == quoteOld.getBid())
         this.quoteState[count] = 0;
       else {
         this.quoteState[count] = 2;
       }
 
       this.listField.invalidate(count);
     }
 
     new Thread(new Runnable()
     {
       public void run() {
         try {
           Thread.sleep(1600L);
         } catch (InterruptedException ex) {
         }
         for (int count = 0; count < RatesListField.this.listData.length; count++) {
           RatesListField.this.quoteState[count] = 0;
           RatesListField.this.listField.invalidate(count);
         }
       }
     }).start();
   }
 
   private class ListCallBack extends AbstractListFieldCallBack
   {
     private Bitmap imageDown = Bitmap.getBitmapResource("down.png");
     private Bitmap ageUpgImportant = Bitmap.getBitmapResource("up.png");
 
     public ListCallBack()
     {
       Bitmap bg = Bitmap.getBitmapResource("row-bg-480x44.png");
       Bitmap bgSelected = Bitmap.getBitmapResource("row-bg-glow2-480x48.png");
 
       setBg(bg);
       setBgSelected(bgSelected);
     }
 
     public void drawListRow(ListField list, Graphics graphics, int index, int y, int w) {
       Quote currentRow = (Quote)get(list, index);
 
       Bitmap icon = null;
 
       Font originalFont = graphics.getFont();
       int originalColor = graphics.getColor();
       int height = list.getRowHeight();
 
       if (RatesListField.this.quoteState[index] == 2)
         icon = this.imageDown;
       else if (RatesListField.this.quoteState[index] == 1) {
         icon = this.ageUpgImportant;
       }
 
       drawBackground(graphics, 0, y, w, height, RatesListField.this.listField.getSelectedIndex() == index);
       int totalImageWidth = height;
       if (icon != null) {
         drawImage(graphics, 135, y, height, icon);
       }
 
       drawPrice(graphics, totalImageWidth + 5, y, w - 5, height, currentRow.getSymbol(), currentRow.getBid(), currentRow.getAsk(), RatesListField.this.listField.getSelectedIndex() == index, RatesListField.this.quoteState[index]);
 
       graphics.setFont(originalFont);
       graphics.setColor(originalColor);
     }
 
     private void drawPrice(Graphics graphics, int x, int y, int w, int h, String symbolName, double priceValue, double priceValue2, boolean selected, int state)
     {
       int fontHeight = 24;
       Font font = DefaultController.getComponentFont().derive(0, fontHeight);
 
       graphics.setColor(16119285);
       graphics.setFont(font.derive(0, fontHeight));
       graphics.drawText(symbolName, 16, y + (h - 2 - fontHeight) / 2, 0x30 | 0x6);
 
       Symbol symbol = Model.getModel().getSettings().getRate().getSymbol(symbolName);
       if (symbol == null)
       {
         symbol = DefaultController.getStaticSymbol(symbolName);
         if (symbol == null) {
           return;
         }
 
       }
 
       if (selected) {
         if (state == 1)
         {
           graphics.setColor(3231753);
         } else if (state == 2)
         {
           graphics.setColor(9047052);
         }
         else graphics.setColor(0);
 
       }
       else if (state == 1) {
         graphics.setColor(9498256);
       }
       else if (state == 2) {
         graphics.setColor(16711680);
       }
       else
       {
         graphics.setColor(16119285);
       }
 
       x = 167;
       int minWidth = 120;
 
       Price price = new Price(symbol, priceValue);
 
       graphics.setFont(this.small);
 
       graphics.drawText(price.getPippetteString(), x, y + (h - 2 - fontHeight) / 2, 0x30 | 0x5, minWidth);
       graphics.setFont(this.bigger);
       graphics.drawText(price.getMainPriceString(), x - this.horizontalPadding - this.small.getAdvance(price.getPippetteString()), y + (h - 2 - fontHeight) / 2, 0x30 | 0x5, minWidth);
 
       graphics.setFont(this.normal);
       graphics.drawText(price.getBaseString(), x - this.horizontalPadding - this.bigger.getAdvance(price.getMainPriceString()) - this.small.getAdvance(price.getPippetteString()), y + (h - 11), 0x28 | 0x5, minWidth);
 
       price = new Price(symbol, priceValue2);
       graphics.setFont(this.normal);
       x = 342;
       graphics.setFont(this.small);
       graphics.drawText(price.getPippetteString(), x, y + this.verticalPadding + (h - 2 - fontHeight) / 2, 0x30 | 0x5, minWidth);
       graphics.setFont(this.bigger);
       graphics.drawText(price.getMainPriceString(), x - this.horizontalPadding - this.small.getAdvance(price.getPippetteString()), y + (h - 2 - fontHeight) / 2, 0x30 | 0x5, minWidth);
 
       graphics.setFont(this.normal);
       graphics.drawText(price.getBaseString(), x - this.horizontalPadding - this.bigger.getAdvance(price.getMainPriceString()) - this.small.getAdvance(price.getPippetteString()), y + (h - 11), 0x28 | 0x5, minWidth);
     }
 
     protected void drawBorder(Graphics graphics, int x, int y, int width, int height)
     {
       graphics.setColor(8421504);
       graphics.drawLine(x - 1, y, x + width - 1, y);
       graphics.drawLine(x - 1, y + height - 1, x + width - 1, y + height - 1);
 
       graphics.drawLine(x - 1, y, x - 1, y + height - 1);
       graphics.drawLine(x + width, y - 1, x + width, y + height - 1);
     }
 
     public Object get(ListField list, int index) {
       return RatesListField.this.listData[index];
     }
   }
}

